package cn.starrysky108.simpleerp.purchase.service.impl;

import cn.starrysky108.simpleerp.article.entity.Article;
import cn.starrysky108.simpleerp.article.service.ArticleService;
import cn.starrysky108.simpleerp.purchase.entity.PurchaseDetail;
import cn.starrysky108.simpleerp.purchase.mapper.PurchaseDetailMapper;
import cn.starrysky108.simpleerp.purchase.service.PurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 采购明细单 服务实现类
 * </p>
 *
 * @author Carl Lee
 */
@Service
@CacheConfig(cacheNames = "purchase")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetail> implements PurchaseService {
    private ArticleService articleService;

    @Override
    @Cacheable(key = "'article'+#id")
    public Article getArticle(Long id) {
        return articleService.getById(id);
    }

    @Override
    @CacheEvict(key = "'article'+#id")
    @Transactional(rollbackFor=Exception .class)
    public void deleted(Long id) throws Exception {
        articleService.removeById(id);
        throw new Exception();
    }

    @Override
    @CachePut(key = "'article'+#entity.id")
    public Article add(Article entity) {
        articleService.saveOrUpdate(entity);
        return entity;
    }

    @Autowired
    public void setDeviceInfoService(ArticleService articleService) {
        this.articleService = articleService;
    }
}
